package com.xxxx.controller;

import com.xxxx.pojo.FastDFSFile;
import com.xxxx.utils.FastDFSClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.io.InputStream;

/**
 * 文件相关 Controller
 *
 * @author HMF
 * @since 1.0.0
 */

@Controller  // 注意这里的@Controller注解
public class FileController {

    private static Logger logger = LoggerFactory.getLogger(FileController.class);

    /**
     * 上传文件
     *
     * @param file
     * @param redirectAttributes
     * @return
     */
    @PostMapping("upload")
    public String FileUpload(@RequestParam("file") MultipartFile file, RedirectAttributes redirectAttributes) {
        // 上传的文件为空时
        if (file.isEmpty()) {
            redirectAttributes.addFlashAttribute("message", "请选择一个文件上传");
            return "redirect:uploadStatus"; // 重定向
        }

        try {
            // 上传文件拿到返回的文件路径
            String path = saveFile(file);
            redirectAttributes.addFlashAttribute("message", "上传成功" + file.getOriginalFilename());
            redirectAttributes.addFlashAttribute("path", "路径：" + path);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/uploadStatus";
    }


    /**
     * 页面跳转
     *
     * @return
     */
    @GetMapping("/uploadStatus")
    public String uploadStatus() {
        return "uploadStatus"; // 跳转到上传结果页面
    }


    /**
     * 页面跳转
     *
     * @return
     */
    @GetMapping("/")
    public String upload() {
        return "upload"; // 跳转到上传页面
    }


    public String saveFile(MultipartFile multipartFile) throws Exception {
        String[] fileAbsolutePath = {};
        String fileName = multipartFile.getOriginalFilename();
        String ext = fileName.substring(fileName.lastIndexOf(".") + 1);
        byte[] file_buff = null;
        InputStream inputStream = multipartFile.getInputStream();
        if (inputStream != null) {
            int len1 = inputStream.available();
            file_buff = new byte[len1];
            inputStream.read(file_buff);
        }
        inputStream.close();
        FastDFSFile file = new FastDFSFile(fileName, file_buff, ext);
        fileAbsolutePath = FastDFSClient.upload(file);
        if (fileAbsolutePath == null) {
            logger.error("上传失败");
        }
        String path = FastDFSClient.getTrackerUrl() + fileAbsolutePath[0] + "/" + fileAbsolutePath[1];
        return path;
    }

}